package com.personalsystem.function.action;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.personalsystem.function.bean.Functions;
import com.personalsystem.function.dao.FunctionDao;
import com.personalsystem.function.service.FunctionService;
import com.personalsystem.function.util.FunctionUtil;
import com.personalsystem.tool.BackJson;
import com.personalsystem.tool.hibernate.Updater;
import com.personalsystem.tool.hibernate.Updater.UpdateMode;
import com.personalsystem.tool.page.Page;



@Controller
public class FunctionAction {
	
	@Autowired
	@Qualifier("functionService")
	private FunctionService service;
	
	@Autowired
	@Qualifier("functionDao")
	private FunctionDao dao;
	
	
	@RequestMapping("/function/loadFunctionTree.do")
	public void loadFunctionTree(HttpServletRequest request,HttpServletResponse response){
		System.out.println("load tree");
		List<Functions> list=dao.getList();
		String jsonString=new FunctionUtil().getTreeJson(list);
		System.out.println(jsonString);
		BackJson.backJsonToHTML(response, "["+jsonString+"]");
		
	}
	
	@RequestMapping("/function/loadTable.do")
	public void loadFunctionTable(int start,int limit,Integer pid,HttpServletRequest request,HttpServletResponse response){
		Page page=null;
		if(pid==null){
			page=dao.getPage(start+1, limit);
		}else{
			page=dao.getPageByPid(start+1, limit, pid);
		}
		String jsonString=new FunctionUtil().getTableJson(page);
		BackJson.backJsonToHTML(response, jsonString);
	}
	
	@RequestMapping("/function/addTree.do")
	public void addTree(Functions function,HttpServletRequest request,HttpServletResponse response){
		if(function.getId()==null){
			function.setStatus(1);
			service.save(function);
		}else{
			Updater<Functions> updater=new Updater<Functions>(function,UpdateMode.MIDDLE);
			service.updateByUpdater(updater);
		}
		
		//service.saveXX();
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}
	
	@RequestMapping("/function/loadFunction.do")
	public void loadFunction(long id,HttpServletRequest request,HttpServletResponse response){
		Functions functions=service.findById(id);
		Map map=new TreeMap();
		map.put("success", true);
		map.put("data", functions);
		JSONObject object=new JSONObject();
		object.putAll(map);
		BackJson.backJsonToHTML(response, object.toString());
	}
	
	@RequestMapping("/function/updateTree.do")
	public void updateTree(Functions function,HttpServletRequest request,HttpServletResponse response){
		service.update(function);
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}
	
	@RequestMapping("/function/deleteTree.do")
	public void deleteTree(String idx,HttpServletRequest request,HttpServletResponse response){
		String[] arr=idx.split(",");
		List<Long> list=new ArrayList<Long>();
		for(String s:arr){
			if(!s.equals("")){
				list.add(Long.parseLong(s));
			}
		}
		Long[] lArr=new Long[list.size()];
		for(int i=0;i<list.size();i++){
			lArr[i]=list.get(i);
		}
		service.deleteById(lArr);
		BackJson.backJsonToHTML(response, "{success:true,msg:'保存成功'}");
	}

}
